package com.bw.router

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.bw.common.path.RouthPath
import com.tencent.mmkv.MMKV

/**
 * 自定义路由拦截器
 */
@Interceptor(priority = 4, name = "路由Token拦截器")
class RouteTokenInterceptor:IInterceptor {
    private lateinit var mContext:Context
    //需要拦截下来的path
    private val needInterceptorPaths = arrayOf(RouthPath.PATH_MAIN_PAGE)
    override fun init(context: Context?) {
        if(context!=null){
            mContext = context
        }

    }
    //做拦截操作
    override fun process(postcard: Postcard, callback: InterceptorCallback) {
       //通过判断path来决定是否需要拦截
        try {
            if(needInterceptorPaths.contains(postcard.path)){
                var token = MMKV.defaultMMKV().decodeString("token","")
                if(token.isNullOrEmpty()){
                    postcard.group=RouthPath.GROUP_LOGIN
                    postcard.path=RouthPath.PATH_LOGIN_PAGE
                    postcard.navigation()
                }else{
                    callback.onContinue(postcard)
                }
            }else{
                //继续执行跳转逻辑
                callback?.onContinue(postcard)
            }
        }catch (e:Exception){
            e.printStackTrace()
            callback.onContinue(postcard)
        }



    }

}